package com.fs.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.fs.dao.ProductDetailsDao;
import com.fs.dao.ShopCartDao;
import com.fs.entity.Product;
import com.fs.entity.Receipt;
import com.fs.entity.ShopCart;
@WebServlet("/shopcart.do")
public class ShopCartServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
			PrintWriter out=resp.getWriter();
			String type=req.getParameter("type");
			ShopCartDao  shopDao=new ShopCartDao();
			//ShopCart shopcart=requestGetDate(req);
			HttpSession session=req.getSession();
			JSONObject json = new JSONObject();
			//int user_id = Integer.parseInt(req.getParameter("user_id").toString());
			 int user_id=0;
			 if(session.getAttribute("user_id")!=null){
					user_id=(Integer) session.getAttribute("user_id");
				}
			if("add".equals(type)){
				String sp_name = req.getParameter("sp_name");
				String sp_price =req.getParameter("sp_price");
				String sp_image   = req.getParameter("sp_image");
				ShopCart shopcart= new ShopCart();
				 shopcart.setUser_id(user_id);
				 shopcart.setProduct_name(sp_name);
				 shopcart.setProduct_pic(sp_image);
				 shopcart.setProduct_price(sp_price);
				 int product_id =shopDao.add(shopcart);
				 session.setAttribute("product_id", product_id);
				try {
					json.put("code", 0);
					json.put("message", "添加成功");
				} catch (Exception e) {
					json.put("code", 1);
					json.put("message", "添加失败");
					e.printStackTrace();
				}
				out.println(json.toString());
				
			}else if("query".equals(type)){
				
				
				session.getAttribute("product_id");
				List <ShopCart> SPlist = shopDao.queryAllProduct(user_id);
				System.out.println(SPlist);
				req.setAttribute("SPlist",SPlist);
				req.getRequestDispatcher("shopcart.jsp").forward(req, resp);
				/*try {
					JSONArray jsonarray=JSONArray.fromObject(SPlist);
					json.put("SPlist",jsonarray );
					json.put("code", 0);
					json.put("message", "添加成功");
					System.out.println(jsonarray.toString());
				} catch (Exception e) {
					json.put("code", 1);
					json.put("message", "添加失败");
					e.printStackTrace();
				}
				out.println(json.toString());*/
			}
		}
	
		private ShopCart requestGetDate(HttpServletRequest req){
			String product_name = req.getParameter("product_name");
			String product_pic = req.getParameter("product_pic");
			String product_price = req.getParameter("product_price");
			 HttpSession session = req.getSession();
			 int user_id=1;
			 if(session.getAttribute("user_id")!=null){
					user_id=(Integer) session.getAttribute("user_id");
				}
			 ShopCart shopcart= new ShopCart();
			 shopcart.setUser_id(user_id);
			 shopcart.setProduct_name(product_name);
			 shopcart.setProduct_pic(product_pic);
			 shopcart.setProduct_price(product_price);
			 return shopcart;
		}
		
		public static void main(String[] args) {
			ShopCartDao  shopDao=new ShopCartDao();
			List <ShopCart> SPlist = shopDao.queryAllProduct(1);
			System.out.println(SPlist);
		}
}
